package com.eelye.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 获取现在时间
     *
     * @return 返回=时分秒全为0的今天的日期
     */
    public static Date getNowDateShort() {
        Calendar ret = Calendar.getInstance();
        ClearTime(ret);

        return ret.getTime();
    }

    public static String getNowStr(){
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
    }

    public static Date getYesterdayDateShort(){
        Calendar ret = Calendar.getInstance();
        ret.add(Calendar.DAY_OF_MONTH, -1);

        ClearTime(ret);

        return ret.getTime();
    }

    static public void ClearTime(Calendar ret){
        ret.set(Calendar.HOUR_OF_DAY , 0);
        ret.set(Calendar.MINUTE , 0 );
        ret.set( Calendar.SECOND , 0 );
        ret.set( Calendar.MILLISECOND , 0 );
    }

    /**
     * 取得给定日期后一天的日期字符串，如果传空，就返回19700101
     */
    static public String getNextDateString(Date date) {
        if (null == date) {
            date = new Date(0);
        }else{
            date = DateUtils.addDays(date, 1);
        }
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        return fmt.format(date);
    }

    public static Date parse(String dateStr) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
    }
}
